import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Window 2.1
import QtQuick.Layouts 1.1
import QtQuick.Controls.Styles 1.1

import Enginio 1.0


Window {
    title: "Coach Assistant"
    width: 400
    height: 640
    id: mainWindow
    visible: true

    property variant runWindow : Qt.createComponent("RunWindow.qml").createObject(this)
    property variant scheduleWindow : Qt.createComponent("ScheduleWindow.qml").createObject(this)
    property variant settingsWindow : Qt.createComponent("SettingsWindow.qml").createObject(this)
    property variant exercisesWindow : Qt.createComponent("ExercisesWindow.qml").createObject(this)


    Rectangle {
        id: mainRect
        anchors.fill: parent
        focus: true


        Header {
            id: header
            title: "Coach Assistant"
            iconVisible: false
        }

        Item {
            id: mainItem
            anchors.top: header.bottom
            width: mainWindow.width
            height: mainWindow.height - header.height

            anchors.horizontalCenter: mainWindow.horizontalCenter

            ColumnLayout {
                id: layout
                anchors.fill: parent
                spacing: 6

                AppButton {
                    id: runButton
                    text: "Run"
                    onClicked: {
                        mainWindow.hide()
                        mainWindow.runWindow.show();
                        console.log(text)
                    }
                }
                AppButton {
                    id: scheduleButton
                    text: "Schedule"
                    onClicked: {
                        mainWindow.hide()
                        mainWindow.scheduleWindow.show();
                        console.log(text)
                    }
                }
                AppButton {
                    id: exerciseButton
                    text: "Exercises"
                    onClicked: {
                        mainWindow.hide()
                        mainWindow.exercisesWindow.show();
                        console.log(text)
                    }
                }
                AppButton {
                    id: settingsButton
                    text: "Settings"
                    onClicked: {
                        mainWindow.hide()
                        mainWindow.settingsWindow.show();

                        console.log(text)
                    }
                }
            }
        }

        Keys.onReleased: {
            if ( event.key === Qt.Key_Back )
            {
                console.log("Back button captured - wunderbar !")
                mainWindow.close()
            }
        }
    }

    Component.onCompleted: {
        console.log("onCompleted")
        runWindow.backPressed.connect(backPressed)
        scheduleWindow.backPressed.connect(backPressed)
        settingsWindow.backPressed.connect(backPressed)
        exercisesWindow.backPressed.connect(backPressed)

        settingsWindow.defaultIntervalChanged.connect(scheduleWindow.defaultIntervalChanged)
    }

    function backPressed() {
        console.log("backPressed")
        runWindow.hide()
        scheduleWindow.hide()
        settingsWindow.hide()
        exercisesWindow.hide()
        mainWindow.show()
        mainRect.focus = true
    }
}
